package cn.iocoder.yudao.module.ent.controller.admin.tag;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.ent.controller.admin.tag.vo.tag.TagPageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.tag.vo.tag.TagRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.tag.vo.tag.TagSaveReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.tag.vo.tagset.TagSetPageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.tag.vo.tagset.TagSetRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.tag.vo.tagset.TagSetSaveReqVO;
import cn.iocoder.yudao.module.ent.dal.dataobject.tag.TagDO;
import cn.iocoder.yudao.module.ent.dal.dataobject.tag.TagSetDO;
import cn.iocoder.yudao.module.ent.service.tag.TagService;
import cn.iocoder.yudao.module.ent.service.tag.TagSetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "企业大数据 - 企业标签")
@RestController
@RequestMapping("/ent/tag-set")
@Validated
public class TagSetController {

    @Resource
    private TagSetService tagSetService;

    @Resource
    private TagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建企业标签组")
    @PreAuthorize("@ss.hasPermission('ent:tag:create')")
    public CommonResult<Long> createTagSet(@Valid @RequestBody TagSetSaveReqVO createReqVO) {
        return success(tagSetService.createTagSet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业标签")
    @PreAuthorize("@ss.hasPermission('ent:tag:update')")
    public CommonResult<Boolean> updateTagSet(@Valid @RequestBody TagSetSaveReqVO updateReqVO) {
        tagSetService.updateTagSet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ent:tag:delete')")
    public CommonResult<Boolean> deleteTagSet(@RequestParam("id") Long id) {
        tagSetService.deleteTagSet(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ent:tag:query')")
    public CommonResult<TagSetRespVO> getTagSet(@RequestParam("id") Long id) {
        TagSetDO tag = tagSetService.getTagSet(id);
        return success(BeanUtils.toBean(tag, TagSetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业标签分页")
    @PreAuthorize("@ss.hasPermission('ent:tag:query')")
    public CommonResult<PageResult<TagSetRespVO>> getTagSetPage(@Valid TagSetPageReqVO pageReqVO) {
        PageResult<TagSetDO> pageResult = tagSetService.getTagSetPage(pageReqVO);
        List<TagSetDO> pageResultList = pageResult.getList();
        PageResult<TagSetRespVO> result = new PageResult<>();
        result.setTotal(pageResult.getTotal());
        result.setList(Collections.emptyList());
        if (CollectionUtil.isNotEmpty(pageResultList)) {
            List<TagDO> list = tagService.getTagListBySetId(pageResultList.stream().map(TagSetDO::getId).collect(Collectors.toList()));
            result.setList(pageResultList.stream().map(labelSet -> {
                TagSetRespVO respVO = BeanUtils.toBean(labelSet, TagSetRespVO.class);
                respVO.setTagList(list.stream().filter(label -> ObjectUtil.equal(label.getSetId(), labelSet.getId())).collect(Collectors.toList()));
                return respVO;
            }).collect(Collectors.toList()));
        }
        return success(result);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取标签列表")
    public CommonResult<List<TagSetRespVO>> getTagSetSimpleList() {
        List<TagSetDO> result = tagSetService.getTagSetList();
        return success(BeanUtils.toBean(result, TagSetRespVO.class));
    }


}